import Types from '../types'
import FavoriteDao from '../../expand/dao/FavoriteDao'
import ProjectModel from '../../model/ProjectModel'

/**
 * 加载收藏的项目
 * @param flag 标识
 * @param isShowLoading   是否显示loading
 * @return {Function}
 */
export function onLoadFavoriteData(flag, isShowLoading) {
	return dispatch => {
		//这里做一个判断，如果监听底部tabBar切换时候，就不要刷新动画
		if (isShowLoading) {
			dispatch({ type: Types.FAVORITE_LOAD_DATA, storeName: flag })
		}
		new FavoriteDao(flag)
			.getAllItems()
			.then(items => {
				let resultData = []
				for (let i = 0; i < items.length; i++) {
					resultData.push(new ProjectModel(items[i], true))
				}
				dispatch({
					type: Types.FAVORITE_LOAD_SUCCESS,
					projectModels: resultData,
					storeName: flag
				})
			})
			.catch(e => {
				console.log(e)
				dispatch({
					type: Types.FAVORITE_LOAD_FAIL,
					error: e,
					storeName: flag
				})
			})
	}
}
